package com.llkj.qingnianguoxue.circle;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.MemberWarningAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.bean.CircleListBean;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.DialogUtil;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.RxBus;
import com.llkj.qingnianguoxue.util.ToastUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetGroupInfoCallback;
import cn.jpush.im.android.api.model.GroupInfo;
import cn.jpush.im.android.api.model.Message;
import cn.jpush.im.api.BasicCallback;

public class MemberWarningActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener{

    @BindView(R.id.refresh_layout)
    protected SwipeRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    protected RecyclerView lvMembers;
    private MemberWarningAdapter mMemberAdapter;
    private List<CircleListBean.CircleBean> mMemberData;//成员数据
    private int totalSize, mPage,mCurrentPosition;
    private int mType;//1禁言，默认警告
    private List<String> mDeniedData;
    private String mGroupChatId;//群组聊天id

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_warning);
        setTitle("圈子成员", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1, -1);
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        ButterKnife.bind(this);

        initView();
        loadWaitDialog();
        onRefresh();
    }

    private void initView(){
        Intent intent = getIntent();
        mType = intent.getIntExtra("type",0);
        mGroupChatId = intent.getStringExtra("groupChatId");

        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvMembers.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mMemberData = new ArrayList<>();
        mMemberAdapter = new MemberWarningAdapter(R.layout.item_member_warning,mMemberData);
        mMemberAdapter.setType(mType);
        lvMembers.setAdapter(mMemberAdapter);

        getData();

        refreshLayout.setOnRefreshListener(this);
        mMemberAdapter.setOnLoadMoreListener(this);
        lvMembers.addOnItemTouchListener(new OnItemChildClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                mCurrentPosition = i;
                final CircleListBean.CircleBean item = mMemberData.get(mCurrentPosition);
                if(view.getId()== R.id.tv_warning){
                    if(mType==1){//禁言
                        DialogUtil.deniedGroupDialog(MemberWarningActivity.this, mDeniedData, new DialogUtil.DialogRechargeClickListener() {
                            @Override
                            public void rechargeClick(int type,final String position) {
                                //设置群成员禁言状态
                                JMessageClient.getGroupInfo(Long.parseLong(mGroupChatId), new GetGroupInfoCallback() {
                                    @Override
                                    public void gotResult(int responseCode, String responseMessage, GroupInfo groupInfo) {
                                        if (0 == responseCode) {
                                            String chatId = "qcgx_"+item.getId();
                                            groupInfo.setGroupMemSilence(chatId, "", true, new BasicCallback() {
                                                @Override
                                                public void gotResult(int i, String s) {
                                                    if (0 == i) {
                                                        loadWaitDialog();
                                                        HttpMethod.circleAnExcuse(MemberWarningActivity.this,MemberWarningActivity.this,mGroupChatId,item.getId(),position);
                                                    } else {
                                                        ToastUtil.makeLongText(MemberWarningActivity.this,"设置禁言失败");
                                                    }
                                                }
                                            });
                                        } else {
                                            ToastUtil.makeLongText(MemberWarningActivity.this,"设置禁言失败");
//                                            Toast.makeText(getApplicationContext(), keepSilence ? "设置禁言失败" : "取消禁言失败", Toast.LENGTH_SHORT).show();
                                            Log.e(TAG, "getGroupInfo failed " + ", responseCode = " + responseCode + " :Desc = " + responseMessage);
                                        }
                                    }
                                });

                            }
                        }).show();
                    }else{//警告
                        DialogUtil.dialogConfirm(MemberWarningActivity.this, "确定警告此用户吗?", null, "取消", "确定", new DialogUtil.DialogClickListener() {
                            @Override
                            public void dialogClick(int type) {
                                if(type==1){//确定
                                    try {
                                        loadWaitDialog();
                                        HashMap<String,String> values = new HashMap<>();
                                        values.put("warn_user","1");
                                        values.put("warn_user_name",item.getUsername());
                                        final Message message = JMessageClient.createGroupCustomMessage(Long.parseLong(mGroupChatId),values);
                                        message.setOnSendCompleteCallback(new BasicCallback() {
                                            @Override
                                            public void gotResult(int responseCode, String s) {
                                                dismissWaitDialog();
                                                if (responseCode == 0) {
                                                    //消息发送成功
                                                    RxBus.getInstance().post(message);
                                                    ToastUtil.makeLongText(MemberWarningActivity.this,"已发送警告");
                                                } else {
                                                    //消息发送失败
                                                }
                                            }
                                        });
                                        JMessageClient.sendMessage(message);
                                    }catch (Exception e){
                                        dismissWaitDialog();
                                    }
                                }
                            }
                        }).show();
                    }
                }
            }
        });
    }

    private void getData() {
        mDeniedData = new ArrayList<>();
        mDeniedData.add("5");
        mDeniedData.add("10");
        mDeniedData.add("15");
        mDeniedData.add("20");
    }

    @Override
    public void onLoadMore() {
        mPage++;
        getMemberData();
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getMemberData();
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, mMemberAdapter, null, lvMembers, totalSize);
    }

    /**
     * 获取成员
     */
    private void getMemberData(){
        HttpMethod.circleMember(this,this,"",mGroupChatId, String.valueOf(mPage));
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_CIRCLE_MEMBER) {//圈子成员
            try {
                CircleListBean circleListBean = GsonUtil.GsonToListBean(data, CircleListBean.class);
                if (circleListBean != null) {
                    if (mPage == 1) {
                        mMemberData.clear();
                        LoadDataHelper.refreshComplete(mMemberAdapter, mMemberData, refreshLayout, lvMembers);
                    } else {
                        mMemberAdapter.setEnableLoadMore(true);
                        mMemberAdapter.loadMoreComplete();
                    }
                    mMemberData.addAll(circleListBean.getMember());
                    mMemberAdapter.notifyDataSetChanged();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(method == HttpMethod.HTTP_CIRCLE_AN_EXCUSE){//禁言
            ToastUtil.makeLongText(this,"已禁言该用户");
        }
    }
}
